package com.allwees.bs.module.pms.service.impl;

import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.pms.dao.ProductOnoffLogDao;
import com.allwees.bs.module.pms.dto.ProductDto;
import com.allwees.bs.module.pms.entity.ProductOnoffLog;
import com.allwees.bs.module.pms.req.query.ProductOnoffLogQueryReq;
import com.allwees.bs.module.pms.service.ProductOnoffLogService;
import com.allwees.core.common.constant.EStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 *
 * @author yangylsky
 * @since 2019-12-19
 *
 */
@Service
public class ProductOnoffLogServiceImpl extends BoServiceImpl<ProductOnoffLogDao, ProductOnoffLog> implements ProductOnoffLogService {
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean logPuton(ProductDto product) {
		product.assertStatus(EStatus.NORMAL);
		ProductOnoffLog onoffLog = new ProductOnoffLog()
				.setProductUuid(product.getUuid())
				.setSupplierUuid(product.getSupplierUuid())
				.setRetailerUuid(product.getRetailerUuid())
				.setActionStatus(EStatus.NORMAL)
				.setOperatorUuid(product.getOperatorUuid());
		return save(onoffLog);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean logPutoff(ProductDto product) {
		product.assertStatus(EStatus.PUTOFF_ONESELF, EStatus.PUTOFF_QA_FAILURE, EStatus.PUTOFF_SELLOUT);
		ProductOnoffLog onoffLog = new ProductOnoffLog()
				.setProductUuid(product.getUuid())
				.setSupplierUuid(product.getSupplierUuid())
				.setRetailerUuid(product.getRetailerUuid())
				.setActionStatus(product.getEStatus())
				.setOperatorUuid(product.getOperatorUuid())
				.setReason(product.getReason())
				.setRemark(product.getRemark());
		return save(onoffLog);
	}

	@Override
	public boolean logForbidSale(ProductDto product) {
		ProductOnoffLog onoffLog = new ProductOnoffLog()
				.setProductUuid(product.getUuid())
				.setSupplierUuid(product.getSupplierUuid())
				.setRetailerUuid(product.getRetailerUuid())
				.setActionStatus(EStatus.DISABLED_FORBID_SALE)
				.setOperatorUuid(product.getOperatorUuid());
		return save(onoffLog);
	}

	@Override
	public boolean logLiftBan(ProductDto product) {
		ProductOnoffLog onoffLog = new ProductOnoffLog()
				.setProductUuid(product.getUuid())
				.setSupplierUuid(product.getSupplierUuid())
				.setRetailerUuid(product.getRetailerUuid())
				.setActionStatus(EStatus.NORMAL)
				.setOperatorUuid(product.getOperatorUuid());
		return save(onoffLog);
	}

	@Override
	public List<ProductOnoffLog> findOnoffLog(ProductOnoffLogQueryReq req) {
		return baseMapper.findOnoffLog(req);
	}
}
